package com.xtz.service.impl;


import cn.hutool.core.lang.Snowflake;
import com.xtz.domain.DishFlavor;
import com.xtz.domain.SetMeal;
import com.xtz.domain.SetmealDishes;
import com.xtz.dto.SetMealDto;
import com.xtz.mapper.SetmealMapper;
import com.xtz.service.SetmealService;
import com.xtz.vo.DishVo;
import com.xtz.vo.SetmealVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * date: 2022/5/13 11:50
 *
 * @author xietianze
 */
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加套餐
     * @return
     */
    @Override
    public Boolean addSetmeal(SetMealDto dto) {
        String idStr = new Snowflake().nextIdStr();
        dto.setId(idStr);
        dto.setType(1);
        dto.setCode("null");
        dto.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        dto.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        dto.setCreateUser("1");
        dto.setUpdateUser("1");

        setmealMapper.addSetmeal(dto);

        List<SetmealDishes> setmealDishes = dto.getSetmealDishes();
        for (SetmealDishes setmealDish : setmealDishes) {
            setmealDish.setSetmealId(dto.getId());
            String idStr2 = new Snowflake().nextIdStr();
            setmealDish.setId(idStr2);
            setmealDish.setSort(1);
            setmealDish.setCreateTime(new Date());
            setmealDish.setUpdateTime(new Date());
            setmealDish.setCreateUser("1");
            setmealDish.setUpdateUser("1");
            setmealDish.setIsDeleted(0);

            setmealMapper.addSetmealDishes(setmealDish);
        }
        return true;
    }

    /**
     * 分页
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public List<SetmealVo> getPageByName(Integer page, Integer pageSize, String name) {
        page = (page - 1) * pageSize;
        return setmealMapper.page(page, pageSize, name);
    }

    /**
     * 总条数
     * @param name
     * @return
     */
    @Override
    public Long selectCountByName(String name) {
        return setmealMapper.selectCountByName(name);
    }

    @Override
    public Boolean updatestatus(Integer status, List<String> ids) {
        return setmealMapper.updatestatus(status, ids)>0;
    }

    @Override
    public SetMeal selectsetmealById(String id) {
        return setmealMapper.selectById(id);
    }

    @Override
    public List<SetmealDishes> selectmealdishBysetmealId(String setmealId) {
        return setmealMapper.selectBysetmealId(setmealId);
    }

    @Override
    public Boolean update(SetMeal setMeal) {
        //修改套餐
        //删除旧套餐菜品
        int i = setmealMapper.deletesetmealdishesById(setMeal.getId());
        //添加新套餐菜品
        List<SetmealDishes> setmealDishes = setMeal.getSetmealDishes();
        String id = setMeal.getId();
        for (SetmealDishes setmealDish : setmealDishes) {
            String idStr = new Snowflake().nextIdStr();
            setmealDish.setId(idStr);
            setmealDish.setDishId(setMeal.getId());
            setmealDish.setCreateUser("1");
            setmealDish.setUpdateUser("1");
            setmealDish.setCreateTime(new Date());
            setmealDish.setUpdateTime(new Date());
            setmealDish.setIsDeleted(0);
            setmealDish.setSort(1);
            setmealDish.setSetmealId(id);
            setmealMapper.addSetmealDishes(setmealDish);
        }

        //修改菜品

        setMeal.setUpdateUser("11");
        setMeal.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));


        return setmealMapper.updatasetmeal(setMeal) >0;
    }

    @Override
    public List<SetMeal> getList(String categoryId, Integer status) {
        return setmealMapper.getList(categoryId,status);
    }
}
